package com.dingreading.cloud.common.util;

import com.dingreading.cloud.common.dto.LoginDataDto;
import com.dingreading.cloud.common.dto.StaffRoleDto;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 账号功能
 */
@Component
public class AccountUtil {

    // 正式的自营门店的公司uid
    public static final String SELF_CORP_UID = "211001";
    // 测试的自营门店的公司uid
    public static final String SELF_CORP_UID_T = "211002";

    // 吴娟uid
    public static final String WuJuanUid = "b4a93927e13311eebaf200163e2142fa";

    // 万科蓝山
    public static final String WKLS_UID = "ihybq5cyk781h7noon1whwck029zd1vk";
    // 清华园
    public static final String QHY_UID = "d119eda5b01b11ec811c00163e2142fa";
    // 揽湖苑
    public static final String LHY_UID = "6dabd3f625c84826bdd4eeefdbee9143";
    // 书香门第
    public static final String SXMD_UID = "7af88f7d936e476a88112f8df5517ad8";
    // 水晶公馆
    public static final String SJGG_UID = "4ce70a7a4ac711eebaf200163e2142fa";
    // 蜀山玫瑰园
    public static final String SSMG_UID = "e1dfedd4b45811eebaf200163e2142fa";
    // 武汉帝大御璟城店
    public static final String WHDD_UID = "e49164cfba6111eebaf200163e2142fa";
    // 保利香槟国际
    public static final String BLXB_UID = "7f7f02093d9411efbaf200163e2142fa";
    // 龙湖天街店
    public static final String LHTJ_UID = "89d78d9b6f2811efbaf200163e2142fa";
    // 水墨兰庭店
    public static final String SMLT_UID = "d84572f56f1711efbaf200163e2142fa";
    // 滨湖银泰店
    public static final String BHYT_UID = "e7bf8027be8c11efbaf200163e2142fa";
    // 岸上玫瑰店
    public static final String ASMG_UID = "d751e33ebe8d11efbaf200163e2142fa";
    // 望湖城店
    public static final String WHC_UID = "0d7c5af1be8e11efbaf200163e2142fa";

    // 蜀山玫瑰园店
    public static final String CORP_UID_6 = "240108";
    // 武汉帝大御璟城店
    public static final String CORP_UID_7 = "240109";
    // 保利香槟国际
    public static final String CORP_UID_8 = "240701";
    // 水墨兰庭店
    public static final String CORP_UID_10 = "240906";

    // 自营门店uid（财务）
    public static List<String> selfStoreUidList = Arrays.asList(
            WKLS_UID, QHY_UID, LHY_UID, SXMD_UID, SJGG_UID, LHTJ_UID
    );

    // 联营的公司uid
    public static List<String> jointOperationCorpUidList = Arrays.asList(
            CORP_UID_6, CORP_UID_7, CORP_UID_8, CORP_UID_10
    );

    /**
     * 是不是体验账号
     */
    public static boolean isTyUser(String corpUid) {
        boolean b = corpUid.startsWith("20");
        return b;
    }

    /**
     * 判断是不是自营门店
     */
    public static boolean isSelfStore(String corpUid) {
        return StringUtils.isNotBlank(corpUid) && corpUid.equals(SELF_CORP_UID);
    }

    /**
     * 判断是不是自营店
     */
    public static boolean isSelfStore(Integer operationType) {
        return operationType != null && operationType.equals(EnumOperationType.SELF.getType());
    }

    /**
     * 判断是不是自营或联营店
     */
    public static boolean isSelfAndConsortiumStore(Integer operationType) {
        return operationType != null &&
                (operationType.equals(EnumOperationType.SELF.getType()) || operationType.equals(EnumOperationType.CONSORTIUM.getType()));
    }

    /**
     * 判断是不是门店店长
     */
    public static boolean isStoreManager(String roles) {
        return roles.contains(",50,");
    }

    /**
     * 判断是不是门店店长
     */
    public static boolean isStoreManager(List<LoginDataDto.StoreRolesBean> storeRolesBeans) {
        if (CollectionUtils.isNotEmpty(storeRolesBeans)) {
            LoginDataDto.StoreRolesBean storeRolesBean = storeRolesBeans.get(0);
            if (CollectionUtils.isNotEmpty(storeRolesBean.getRoles())) {
                return storeRolesBean.getRoles().contains("50");
            }
        }
        return false;
    }

    /**
     * 判断是不是门店店长或顾问
     */
    public static boolean isManagerOrAdvisor(List<LoginDataDto.StoreRolesBean> storeRolesBeans) {
        if (CollectionUtils.isNotEmpty(storeRolesBeans)) {
            LoginDataDto.StoreRolesBean storeRolesBean = storeRolesBeans.get(0);
            if (CollectionUtils.isNotEmpty(storeRolesBean.getRoles())) {
                return storeRolesBean.getRoles().contains("21") || storeRolesBean.getRoles().contains("50");
            }
        }
        return false;
    }


    /**
     * 判断用户角色
     */
    public static int isStaffRole(List<LoginDataDto.StoreRolesBean> storeRolesBeans) {
        if (CollectionUtils.isNotEmpty(storeRolesBeans)) {
            LoginDataDto.StoreRolesBean storeRolesBean = storeRolesBeans.get(0);
            if (CollectionUtils.isNotEmpty(storeRolesBean.getRoles())) {
                if (storeRolesBean.getRoles().contains("50")) {
                    return 1;
                } else if (storeRolesBean.getRoles().contains("22") || storeRolesBean.getRoles().contains("32")) {
                    return 2;
                }
            }
        }
        return 0;
    }

    /**
     * 判断用户角色
     */
    public static StaffRoleDto getStaffRole(List<LoginDataDto.StoreRolesBean> storeRolesBeans) {
        if (CollectionUtils.isNotEmpty(storeRolesBeans)) {
            LoginDataDto.StoreRolesBean storeRolesBean = storeRolesBeans.get(0);
            if (CollectionUtils.isNotEmpty(storeRolesBean.getRoles())) {
                List<String> roleList = storeRolesBean.getRoles();
                StaffRoleDto roleDto = new StaffRoleDto();
                if (roleList.contains("99")) {
                    // 投资人
                    roleDto.setRole(9);
                    roleDto.setIdentify("investor");
                } else if (roleList.contains("50")) {
                    // 店长
                    roleDto.setRole(1);
                    roleDto.setIdentify("manager");
                } else if (roleList.contains("32")) {
                    // 市场组长
                    roleDto.setRole(2);
                    roleDto.setIdentify("marketerTop");
                } else if (roleList.contains("22")) {
                    // 市场
                    roleDto.setRole(3);
                    roleDto.setIdentify("marketer");
                } else if (roleList.contains("21")) {
                    // 顾问
                    roleDto.setRole(4);
                    roleDto.setIdentify("counselor");
                } else if (roleList.contains("30")) {
                    // 导读组长
                    roleDto.setRole(5);
                    roleDto.setIdentify("readerTop");
                } else if (roleList.contains("20")) {
                    // 导读
                    roleDto.setRole(6);
                    roleDto.setIdentify("reader");
                }
                return roleDto;
            }
        }
        return null;
    }

    /**
     * 判断用户角色，传入角色列表
     */
    public static StaffRoleDto getStaffRoleV2(List<String> roleList) {
        if (CollectionUtils.isNotEmpty(roleList)) {
                StaffRoleDto roleDto = new StaffRoleDto();
                if (roleList.contains("99")) {
                    // 投资人
                    roleDto.setRole(9);
                    roleDto.setIdentify("investor");
                } else if (roleList.contains("50")) {
                    // 店长
                    roleDto.setRole(1);
                    roleDto.setIdentify("manager");
                } else if (roleList.contains("32")) {
                    // 市场组长
                    roleDto.setRole(2);
                    roleDto.setIdentify("marketerTop");
                } else if (roleList.contains("22")) {
                    // 市场
                    roleDto.setRole(3);
                    roleDto.setIdentify("marketer");
                } else if (roleList.contains("21")) {
                    // 顾问
                    roleDto.setRole(4);
                    roleDto.setIdentify("counselor");
                } else if (roleList.contains("30")) {
                    // 导读组长
                    roleDto.setRole(5);
                    roleDto.setIdentify("readerTop");
                } else if (roleList.contains("20")) {
                    // 导读
                    roleDto.setRole(6);
                    roleDto.setIdentify("reader");
                }
                return roleDto;
            }
        return null;
    }

    /**
     * 是否市场员工
     */
    public static int isMarketStaff(List<LoginDataDto.StoreRolesBean> storeRolesBeans) {
        if (CollectionUtils.isNotEmpty(storeRolesBeans)) {
            LoginDataDto.StoreRolesBean storeRolesBean = storeRolesBeans.get(0);
            if (CollectionUtils.isNotEmpty(storeRolesBean.getRoles())) {
                List<String> roleList = storeRolesBean.getRoles();
                if (roleList.contains("50")) {
                    return 1;
                } else if (roleList.contains("21")) {
                    return 2;
                } else if (roleList.contains("32")) {
                    return 3;
                } else if (roleList.contains("22")) {
                    return 4;
                }
            }
        }
        return 0;
    }

    /**
     * 判断是否生产环境
     */
    public static boolean judgeProd(String envi) {
        return envi.equals("prod");
    }

    /**
     * 门店合作类型sql
     */
    public static String getCooperationTypeSql(List<Integer> cooperationTypes) {
        String sql = cooperationTypes.stream().map(String::valueOf).collect(Collectors.joining("|"));
        return "cooperation_type REGEXP '(^|,)(" + sql + ")(,|$)'";
    }

    /**
     * 门店运营类型sql
     */
    public static String getOperationTypeSql(Integer operationType) {
        return "operation_type REGEXP '(^|,)(" + operationType + ")(,|$)'";
    }

    /**
     * 门店合作类型sql
     */
    public static String getCooperationTypeSql2(List<Integer> cooperationTypes) {
        return "(^|,)(" + cooperationTypes.stream().map(String::valueOf).collect(Collectors.joining("|")) + ")(,|$)";
    }

    /**
     * 门店运营类型sql
     */
    public static String getOperationTypeSql2(Integer operationType) {
        return "(^|,)(" + operationType + ")(,|$)";
    }

    /**
     * 判断是不是采购或联营店订单
     *
     * @param operationType 运营类型
     * @param staffUid      下单人uid
     */
    public static boolean isJointOperation(Integer operationType, String staffUid) {
        return (operationType.equals(EnumOperationType.SELF.getType()) && staffUid.equals(WuJuanUid)) || operationType.equals(EnumOperationType.CONSORTIUM.getType());
    }

}
